#!/bin/sh

###############################################################
#### This script automatically builds a combined ATLAS/CLAPACK
#### DLL to be used with the Native Library Interface
###############################################################


#####################################################
### VARIABLES you need to customise
#####################################################

## BUILD_DIR: Where you want to "build". Default: /tmp
BUILD_DIR="/tmp"

## URL to download the atlas tar.gz file.
ATLAS_URL="http://internap.dl.sourceforge.net/sourceforge/math-atlas/atlas3.6.0.tar.gz"

## URL to download the clapack.tgz file
CLAPACK_URL="http://www.netlib.org/clapack/clapack.tgz"

## URL to exports.def
EXPORTS_URL="http://www.dnanalytics.net/files/exports.def"

## URL to dsecnd.c
DSECND_URL="http://www.dnanalytics.net/files/dsecnd.c"

## URL to second.c
SECOND_URL="http://www.dnanalytics.net/files/second.c"

#######################################################

do_download() {
       URL=$1
       OUTPUT_FILE=$2
       if [ $3 ]; then ALWAYS="true"; else ALWAYS="false"; fi

       echo "Downloading ${OUTPUT_FILE}..."
       if [ ! -f ${OUTPUT_FILE} -o ${ALWAYS} = "true" ]; then
               wget ${URL} -O ${OUTPUT_FILE}
       else
               echo "No need to download. File already exists"
       fi
}

#######################################################

atlas_download() {
       do_download ${ATLAS_URL} atlas.tar.gz
}

atlas_extract() {
       tar xzf atlas.tar.gz
       cd ATLAS
}

atlas_get_proctype() {
       PROC_TYPE=`ls Make.[^t]* | awk -F"." '{print $2}'`
}

atlas_make_install() {
       make install arch=${PROC_TYPE}
}

clapack_download() {
       do_download ${CLAPACK_URL} clapack.tar.gz
}

clapack_extract() {
       tar xzf clapack.tar.gz
       cd CLAPACK
}

clapack_post_extract() {
       cp INSTALL/make.inc.LINUX ./make.inc
       sed -e 's/^BLASLIB.*/BLASLIB = ..\/..\/libcblaswr.a -L..\/ATLAS\/lib\/${PROC_TYPE} -lcblas -latlas/g' make.inc > make.inc.new
       echo "CFLAGS   += -mno-cygwin -fomit-frame-pointer -DUSE_CLOCK" >> make.inc.new
       echo "LOADOPTS += -Wl,-Bstatic" >> make.inc.new
       mv make.inc.new make.inc
       do_download ${DSECND_URL} ./SRC/dsecnd.c ALWAYS
       do_download ${SECOND_URL} ./SRC/second.c ALWAYS
       find -name 'Makefile' -print -exec sed --in-place=.clapack30 --regexp-extended 's/(^| )-lc($| )/ /' {} \;
       find -name 'Makeblat*' -print -exec sed --in-place=.clapack30 --regexp-extended 's/(^| )-lc($| )/ /' {} \;
}

clapack_make() {
       echo "  ->[a] Make >>f2clib<< ..."
       make f2clib
       echo "  ->[b] Make >>install<< ..."
       make install
       echo "  ->[c] Make >>cblaswrap<< ..."
       make cblaswrap
       echo "  ->[d] Make ..."
       cd SRC
       make
       cd ..
}

clapack_combine() {
       mkdir -p combine
       cd combine
       ar x ../../ATLAS/lib/${PROC_TYPE}/liblapack.a
       ar -x ../libcblaswr.a
       ar -x ../F2CLIBS/libF77.a
       ar -x ../F2CLIBS/libI77.a
       cp ../lapack_LINUX.a ../liblapack.a
       ar -rs ../liblapack.a *.o
       cd ..
}


make_shared(){
       cd ..
       wget ${EXPORTS_URL} -O exports.def
       if [ ! -r "./ATLAS/lib/${PROC_TYPE}/libptcblas.a" ]
       then
       gcc -v -mno-cygwin -shared -o atlas.dll exports.def -Wl,--output-def=atlas.def ./CLAPACK/liblapack.a ./ATLAS/lib/${PROC_TYPE}/libcblas.a ./ATLAS/lib/${PROC_TYPE}/libatlas.a 
       else
       gcc -v -mno-cygwin -shared -o atlas.dll exports.def -Wl,--output-def=atlas.def ./CLAPACK/liblapack.a  ./ATLAS/lib/${PROC_TYPE}/libatlas.a ./ATLAS/lib/Linux_P4SSE2_2/libtstatlas.a ./ATLAS/lib/Linux_P4SSE2_2/libptcblas.a
       fi
}

error_exit() {
    if [ ! "$1" = "" ]
    then
        echo "ERROR: $1"
    else
        echo "ERROR: (no details available)"
    fi
    exit 1
}


main() {
       echo "==============================================================================="
       echo " Auto-Build script for ATLAS (Windows-Version creating a dll)"
       echo "==============================================================================="
       echo ""
        StartDate=`date`
       echo "Start: $StartDate"
       echo ""

       echo "==>[0] Check, create, clean build dir ..."
       echo "-------------------------------------------------------------------------------"
        if [ ! -d $BUILD_DIR ]; then mkdir -p $BUILD_DIR; fi
       cd ${BUILD_DIR}
       ret=$?; if [ $ret -ne 0 ]; then error_exit "Cannot create and cd to build directory ($BUILD_DIR)"; fi

       rm -rf ATLAS CLAPACK

       echo ""
       echo "==>[1] Downloading ATLAS ..."
       echo "-------------------------------------------------------------------------------"
       atlas_download
       ret=$?; if [ $ret -ne 0 ]; then error_exit "ATLAS download did not succeed ($ret)"; fi
       echo ""
       echo "==>[2] Extracting ATLAS ..."
       echo "-------------------------------------------------------------------------------"
       atlas_extract
       ret=$?; if [ $ret -ne 0 ]; then error_exit "ATLAS extraction did not succeed ($ret)"; fi
       echo ""
       echo "==>[3] Configure ATLAS ..."
       echo "-------------------------------------------------------------------------------"
       echo -n "Configuration options will be saved in files \"Conf*.log\" which will be "
       echo    "moved to ${BUILD_DIR}/ATLAS/bin/<ARCH>/INSTALL_LOG/Conf*.log\"."
       make
       ret=$?; if [ $ret -ne 0 ]; then error_exit "ATLAS configuration did not succeed ($ret)"; fi
       echo ""
       echo "==>[4] Get configured ATLAS architecture ..."
       echo "-------------------------------------------------------------------------------"
       atlas_get_proctype
       ret=$?; if [ $ret -ne 0 ]; then error_exit "ATLAS target could not be determined ($ret)"; fi
       echo ""
       echo "==>[5] Make (compile) ATLAS ..."
       echo "-------------------------------------------------------------------------------"
       atlas_make_install
       ret=$?; if [ $ret -ne 0 ]; then error_exit "ATLAS main build did not succeed ($ret)"; fi

       cd ..

       echo ""
       echo "==>[6] Downloading CLAPACK ..."
       echo "-------------------------------------------------------------------------------"
       clapack_download
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK download did not succeed ($ret)"; fi
       echo ""
       echo "==>[7] Extracting CLAPACK ..."
       echo "-------------------------------------------------------------------------------"
       clapack_extract
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK extraction did not succeed ($ret)"; fi

       echo ""
       echo "==>[8] Post-extract steps for CLAPACK ..."
       echo "-------------------------------------------------------------------------------"
       clapack_post_extract
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK post-ectraction steps did not succeed ($ret)"; fi
       echo ""
       echo "==>[9] Make (compile) CLAPACK ..."
       echo "-------------------------------------------------------------------------------"
       clapack_make
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK main build did not succeed ($ret)"; fi

       echo ""
       echo "==>[10] Combining CLAPACK and ATLAS ..."
       echo "-------------------------------------------------------------------------------"
       clapack_combine
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK - ATALS combination did not succeed ($ret)"; fi

       echo ""
       echo "==>[11] Building shared library (dll) ..."
       echo "-------------------------------------------------------------------------------"
       make_shared
       ret=$?; if [ $ret -ne 0 ]; then error_exit "CLAPACK - ATLAS building the DLL did not succeed ($ret)"; fi

       echo ""
       echo "Finished: `date`"
        echo "[Started: $StartDate]"
        echo ""
}

## Run the main program
main
##lib /machine:i386 /def:atlas.def

